package ma.ensao.evalmetrics.view;

import java.awt.Color;
import java.awt.GradientPaint;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import ma.ensao.evalmetrics.controller.ClasseManager;
import ma.ensao.evalmetrics.controller.ResultManager;
import ma.ensao.evalmetrics.model.Result;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionSupport;

public class ProprieteGraphe extends ActionSupport {
	

	private ResultManager resultmanager = new ResultManager();
	private JFreeChart chart;
	static Long id;
	int param;
	List<Result> liste;
	
	public void param() throws NumberFormatException, IOException {

		File file = new File("list.txt");
		InputStream ips = new FileInputStream(file);
		InputStreamReader ipsr = new InputStreamReader(ips);
		BufferedReader br = new BufferedReader(ipsr);
		String ligne;
		int j = 0;
		while ((ligne = br.readLine()) != null) {
			System.out.println(ligne);
			if (j == 0) {
				id = (long) Integer.parseInt(ligne);
				System.out.println("mm" + id);
			}
			if (j == 1) {
				param = Integer.parseInt(ligne);
				System.out.println("lll" + param);
			}
			j++;
		}
		br.close();
		ipsr.close();
		ips.close();
		//file.delete();
	}
	
	public String heritage(){
		try {
			param();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(id + "     " + param);
		liste = resultmanager.MesResultat(id);
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(int i=0;i<liste.size();i++){
			dataset.addValue(liste.get(i).getDit(), "DIT", "test"+i);
		}
		
		 chart = ChartFactory.createBarChart(
		            "Histogramme Des Métriques de l’héritage",         // chart title
		            "Métrique",               // domain axis label
		            "Valeur",                  // range axis label
		            dataset,                  // data
		            PlotOrientation.VERTICAL, // orientation
		            true,                     // include legend
		            true,                     // tooltips?
		            false                     // URLs?
		        );
		 CategoryPlot plot = chart.getCategoryPlot();
	     
		 // set the range axis to display integers only...
		 final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	     rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	     
	     BarRenderer renderer = (BarRenderer) plot.getRenderer();
	     GradientPaint gp0 = new GradientPaint(
	             0.0f, 0.0f, Color.blue, 
	             0.0f, 0.0f, new Color(30, 30, 64)
	     );
	     renderer.setSeriesPaint(0, gp0);
	     CategoryAxis domainAxis = plot.getDomainAxis();
	     domainAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0)
	        );
		
		return SUCCESS;
	}
	
	public String encapsulation(){
		try {
			param();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(id + "     " + param);
		liste = resultmanager.MesResultat(id);
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(int i=0;i<liste.size();i++){
			dataset.addValue(liste.get(i).getAHF(), "AHF", "test"+i);
		}
		
		 chart = ChartFactory.createBarChart(
		            "Histogramme Des Métriques d'encapsulation",         // chart title
		            "Métrique",               // domain axis label
		            "Valeur",                  // range axis label
		            dataset,                  // data
		            PlotOrientation.VERTICAL, // orientation
		            true,                     // include legend
		            true,                     // tooltips?
		            false                     // URLs?
		        );
		 CategoryPlot plot = chart.getCategoryPlot();
	     
		
	     BarRenderer renderer = (BarRenderer) plot.getRenderer();
	     GradientPaint gp0 = new GradientPaint(
	             0.0f, 0.0f, Color.green, 
	             0.0f, 0.0f, new Color(0, 64, 0)
	         );
	     renderer.setSeriesPaint(0, gp0);
	     CategoryAxis domainAxis = plot.getDomainAxis();
	     domainAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0)
	        );
		
		return SUCCESS;
	}
	
	public String complex(){
		try {
			param();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(id + "     " + param);
		liste = resultmanager.MesResultat(id);
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(int i=0;i<liste.size();i++){
			dataset.addValue(liste.get(i).getCyclo(), "Cyclo", "test"+i);
		}
		
		 chart = ChartFactory.createBarChart(
		            "Histogramme Des Métriques de complexité ",         // chart title
		            "Métrique",               // domain axis label
		            "Valeur",                  // range axis label
		            dataset,                  // data
		            PlotOrientation.VERTICAL, // orientation
		            true,                     // include legend
		            true,                     // tooltips?
		            false                     // URLs?
		        );
		 CategoryPlot plot = chart.getCategoryPlot();
		// set the range axis to display integers only...
		 final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	     rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
	     BarRenderer renderer = (BarRenderer) plot.getRenderer();
	     GradientPaint gp0 = new GradientPaint(
	             0.0f, 0.0f, Color.red, 
	             0.0f, 0.0f, Color.black
	         );
	     renderer.setSeriesPaint(0, gp0);
	     CategoryAxis domainAxis = plot.getDomainAxis();
	     domainAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0)
	        );
		
		return SUCCESS;
	}
	
	public String coupling(){
		try {
			param();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(id + "     " + param);
		liste = resultmanager.MesResultat(id);
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(int i=0;i<liste.size();i++){
			dataset.addValue(liste.get(i).getCa(), "Ca", "test"+i);
			dataset.addValue(liste.get(i).getCe(), "Ce", "test"+i);
			dataset.addValue(liste.get(i).getRFC(), "RFC", "test"+i);
		}
		
		 chart = ChartFactory.createBarChart(
		            "Histogramme Des Métriques du coupling",         // chart title
		            "Métrique",               // domain axis label
		            "Valeur",                  // range axis label
		            dataset,                  // data
		            PlotOrientation.VERTICAL, // orientation
		            true,                     // include legend
		            true,                     // tooltips?
		            false                     // URLs?
		        );
		 CategoryPlot plot = chart.getCategoryPlot();
		 BarRenderer renderer = (BarRenderer) plot.getRenderer();
	      //  renderer.setDrawBarOutline(false);
		 GradientPaint gp0 = new GradientPaint(
		            0.0f, 0.0f, Color.blue, 
		            0.0f, 0.0f, new Color(0, 0, 64)
		        );
		        GradientPaint gp1 = new GradientPaint(
		            0.0f, 0.0f, Color.green, 
		            0.0f, 0.0f, new Color(0, 64, 0)
		        );
		        GradientPaint gp2 = new GradientPaint(
		            0.0f, 0.0f, Color.red, 
		            0.0f, 0.0f, new Color(64, 0, 0)
		        );
		        renderer.setSeriesPaint(0, gp0);
		        renderer.setSeriesPaint(1, gp1);
		        renderer.setSeriesPaint(2, gp2);
	     CategoryAxis domainAxis = plot.getDomainAxis();
	     domainAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0)
	        );
		
		return SUCCESS;
	}

	public String abstraction(){
		try {
			param();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(id + "     " + param);
		liste = resultmanager.MesResultat(id);
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(int i=0;i<liste.size();i++){
			dataset.addValue(liste.get(i).getAbs(), "Abs", "test"+i);
		}
		
		 chart = ChartFactory.createBarChart(
		            "Histogramme Des Métriques d'abstraction",         // chart title
		            "Métrique",               // domain axis label
		            "Valeur",                  // range axis label
		            dataset,                  // data
		            PlotOrientation.VERTICAL, // orientation
		            true,                     // include legend
		            true,                     // tooltips?
		            false                     // URLs?
		        );
		 CategoryPlot plot = chart.getCategoryPlot();
	     
		
	     BarRenderer renderer = (BarRenderer) plot.getRenderer();
	     GradientPaint gp0 = new GradientPaint(
                 0f, 0f, Color.WHITE, 200f, 0f, Color.ORANGE);
	     renderer.setSeriesPaint(0, gp0);
	     CategoryAxis domainAxis = plot.getDomainAxis();
	     domainAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0)
	        );
		
		return SUCCESS;
	}
	
	public String size(){
		try {
			param();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(id + "     " + param);
		liste = resultmanager.MesResultat(id);
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(int i=0;i<liste.size();i++){
			dataset.addValue(liste.get(i).getCyclo(), "Cyclo", "test"+i);
			dataset.addValue(liste.get(i).getLOC(), "LOC", "test"+i);
			dataset.addValue(liste.get(i).getCLOC(), "CLOC", "test"+i);
			dataset.addValue(liste.get(i).getNOM(), "NOM", "test"+i);
		}
		
		 chart = ChartFactory.createBarChart(
		            "Histogramme Des Métriques du size",         // chart title
		            "Métrique",               // domain axis label
		            "Valeur",                  // range axis label
		            dataset,                  // data
		            PlotOrientation.VERTICAL, // orientation
		            true,                     // include legend
		            true,                     // tooltips?
		            false                     // URLs?
		        );
		 CategoryPlot plot = chart.getCategoryPlot();
		 BarRenderer renderer = (BarRenderer) plot.getRenderer();
	      //  renderer.setDrawBarOutline(false);
		 GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, new Color(0, 0, 64)
        );
        GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, new Color(0, 64, 0)
        );
        GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red, 
            0.0f, 0.0f, new Color(64, 50, 50)
        );
        GradientPaint gp3 = new GradientPaint(5, 5, Color.red, 20, 20, Color.yellow, true);
        
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);
        renderer.setSeriesPaint(3, gp3);
	     CategoryAxis domainAxis = plot.getDomainAxis();
	     domainAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0)
	        );
		
		return SUCCESS;
	}
	
	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	
}

